<?php

namespace App\Repositories\Eloquent\Biz;

use Illuminate\Container\Container as App;
use App\Repositories\Eloquent\Repository;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Repositories\Eloquent\Mgt\UserRepository as User;
use App\Repositories\Eloquent\Biz\TrackRepository as Track;
use App\Repositories\Eloquent\Biz\RecordRepository as Record;
use App\Repositories\Eloquent\Biz\SummaryRepository as Summary;

class SettleRepository extends Repository
{

    protected $user;

    protected $debug;
    /**
     * @param App
     */
    public function __construct(App $app, User $user)
    {
        parent::__construct($app);

        $this->user = $user;

        //短信调试开关
        $this->debug = env('SMS_DEBUG');

    }

    /**
     * 指定模型名称
     *
     * @return mixed
     */
    function model()
    {
        return 'App\Models\Mgt\User';
    }


    /**
     * [用户登录]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public function login($request)
    {
        if(!$this->debug){
            if($request->smscode != \Session::get('smscode'))
                return $this->responseFail(405, '验证码不正确');
        }


        if ( \Auth::guard()->attempt([
            'mobile' => $request->mobile,
            'password' => DEFAULT_PASSWORD,
            //'type'=$request->type
            ])) {
            return $this->responseSuccess('登录成功', $request, route('wx.settle.index'));
        }

        return $this->responseFail(500, '登录失败');
    }



    /**
     * [用户注册]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public function register($request)
    {
        if(!$this->debug){
            if($request->smscode != \Session::get('smscode'))
                return $this->responseFail(405, '验证码不正确');
        }

        $birthday = date('Ymd',strtotime($request->birthday) );

        if( $this->model::checkIdcard( $request->idcard, $request->type ) )
            return $this->responseFail(405, '身份证号码已注册');

        if( $this->model::checkMobile( $request->mobile, $request->type ) )
            return $this->responseFail(405, '手机号码已注册');

        \DB::beginTransaction();
        try{
            $user = $this->create([
                'name'      => $request->name,
                'account'   => \App\Models\Mgt\Util::generate( $this->model->max('account') ),
                'password'  => \Hash::make(DEFAULT_PASSWORD),
                'mobile'    => $request->mobile,
                'idcard'    => $request->idcard,
                'gender'    => $request->gendar,
                'birthday'  => date('Y-m-d',strtotime( $request->birthday)),
                'school_id' => is_null($request->school) ? 1:$request->school,
                'branch_id' => 1,
                'created_at' => time(),
                'type'      => $request->type,
                'source'    => $request->source,
            ]);
            if ($result = !!$user) {
                $user->summary()->create([
                    'type'          =>$user->type,
                    'updated_at'    =>time()
                ]);

                \Auth::guard()->login($user);
            }

            \DB::commit();

            return $this->responseSuccess('账号注册成功', $user, route('wx.settle.index'));

        } catch (Exception $e) {

            \DB::rollback();

            return $this->responseFail(500,'账号注册失败');
        }

    }


}